import { dateParse, fillByChar } from '../../plugins/utils'

export const enumRecordType = {
  1: '收入',
  2: '支出'
}

// 时间格式化 2019年09月04日 星期三
export const getDateWeekString = date => {
  const dateObj = dateParse(date)
  if (!dateObj || !dateObj.getFullYear) { return '' }

  const year = dateObj.getFullYear()
  const month = dateObj.getMonth() + 1
  const day = dateObj.getDate()
  const week = dateObj.getDay()

  const weekArr = ['日', '一', '二', '三', '四', '五', '六']

  return `${year}年${fillByChar(month)}月${fillByChar(day)}日 星期${weekArr[week]}`
}

export const convert = item => {
  item.icon = item.icon || 'bmlocal://assets/common_icon_002.png'
  item.isIn = +item.account_record_type === 1
  item.remarkStr = item.remark || item.project_detail_des
  item.typeStr = enumRecordType[item.account_record_type] || '-'
  item.dateStr = getDateWeekString(item.account_date)
  return item
}

// 按列分组
export const groupByCols = (list, cols) => {
  return list.reduce((p, c, i) => {
    c.cateIndex = i
    if (i % cols === 0) {
      p.push([c])
    } else {
      p[p.length - 1].push(c)
    }
    return p
  }, [])
}
